package com.kly.pay.task.callBackImpl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.kly.enums.CallBackResultEnum;
import com.kly.enums.PayTypeEnum;
import com.kly.pay.db.entity.POrderEntity;
import com.kly.pay.task.AbstractCallBackSendService;
import com.kly.pay.task.CallBackResult;
import com.kly.pay.task.CallBackTaskService;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Map;


/**
 * 支付回调处理
 * @author linqu
 */
@Service("callBackRepayService")
public class CallBackRepayService extends AbstractCallBackSendService {

    @PostConstruct
    public void init() {
        CallBackTaskService.handlers.put(PayTypeEnum.REPAY, this);
    }

    @Override
    public <T> CallBackResult doCallBack(PayTypeEnum payTypeEnum, T t) throws Exception {
        if (t instanceof POrderEntity) {
            POrderEntity payOrder = (POrderEntity) t;
            final String payExt = payOrder.getPayExt();

            String url = payOrder.getCallbackUrl();
            Map<String, String> map = Maps.newHashMap();
            map.put("orderNo", payOrder.getOrderNo());
            map.put("userCode", payOrder.getUserCode());
            map.put("payOrderNo", payOrder.getPayOrderNo());
            map.put("purchaseToken", payOrder.getPurchaseToken());
            map.put("payGateway", payOrder.getPayGateway());
            map.put("repayStatusExtReq", payExt);

            return super.sendResult(url, map);
        }
        return new CallBackResult().setSuccess(Boolean.FALSE).setCallBackMsg("callBackFailed");
    }
}
